package com.example.utils;

import com.example.entity.vo.PmsCategoryVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：fbb
 * @version ：1.0
 * @since 2021-12-26 15:34
 */
public class TreeUtils01 {
    /**
     * 使用递归方法建树
     * @param treeNodes
     * @return
     */
    public static List<PmsCategoryVo> buildByRecursive(List<PmsCategoryVo> pmsCategoryVos) {
        List<PmsCategoryVo> trees = new ArrayList<PmsCategoryVo>();
        for (PmsCategoryVo pmsCategoryVo : pmsCategoryVos) {
            if (pmsCategoryVo.getParentCid()==1454) {
                trees.add(findChildren(pmsCategoryVo,pmsCategoryVos));
            }
        }
        return trees;
    }
    /**
     * 递归查找子节点
     * @param treeNodes
     * @return
     */
    public static PmsCategoryVo findChildren(PmsCategoryVo pmsCategoryVo, List<PmsCategoryVo> pmsCategoryVos) {
        for (PmsCategoryVo pmsCategoryVo2 : pmsCategoryVos) {
            if(pmsCategoryVo.getCatId().equals(pmsCategoryVo2.getParentCid())) {
                if(pmsCategoryVo.getChildrens() == null) {
                    pmsCategoryVo.setChildrens(new ArrayList<PmsCategoryVo>());
                }
               //是否还有子节点，如果有的话继续往下遍历，如果没有则直接返回
                pmsCategoryVo.getChildrens().add(findChildren(pmsCategoryVo2,pmsCategoryVos));
            }
        }
        return pmsCategoryVo;
    }
}
